<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2013, Chongqing Zero Technology Co. Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Admin\Controller\InitController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
Class ConvertController extends InitController {
      public function _initialize() {
       parent::_initialize();
        $this->db = D('Convert');
        $this->member_db = D('Member');
    }

    public function index(){
    	
        $pagecurr = max(1,I('page',0,'intval')); 
        $pagesize = 10;

        $tmps = $this->db->order('time DESC')->page($pagecurr,$pagesize)->select();


        // for ($i=0; $i < count($tmps) ; $i++) {
        // 	$result[$i] = $this->member_db->where(array('userid'=>$tmps[$i]['userid']))->getField('userid,nickname');

        // }
        $count = $this->db->count(); 
        $pages = page($count, 10);

        //var_dump($result);
        include $this->admin_tpl('convert_list');
    }
    //删除操作
    public function delete(){

        $id = $_GET['id'];
        $result = $this->db->delete($id);
        if($result)
           $this->success('操作成功', 'javascript:close_dialog();', 2);

    }
    /**
     * 审核兑换商品
     * @param   $id 修改记录的ID
     */
    public function update($id = 0){
             if($id){
             	$info = $this->db->where(array('id'=>$id))->find(); 
                include $this->admin_tpl('convert_update');
            }
            if($_POST){
            	$info = I('info');
            	if($info['status'] == 1){
            		$thms = $this->member_db->where(array('userid'=>$info['userid']))->find();
            		if($info['credit_type'] == 'bean'){
            			$c_bean = $thms['u_bean'] - $info['credit_num'];
            			if($c_bean > 0){
            				$tmpsss = $this->member_db->where(array('userid'=>$info['userid']))->setField('u_bean',$c_bean);
            				$info = $this->db->where(array('id'=>$info['id']))->setField('status',$info['status']); 
            				
            				$this->success('操作成功', 'javascript:close_dialog();', 2);
            			}else{
            				$this->error('操作失败！,该用户帐户余额不足');
            			}

            		}
            		if($info['credit_type'] == 'coin'){
            			$coin = $thms['u_coin'] - $info['credit_num'];
            			//var_dump($coin);exit;
            			if($coin > 0){
            				$tmpsss = $this->member_db->where(array('userid'=>$info['userid']))->setField('u_coin',$coin);
            				$info = $this->db->where(array('id'=>$info['id']))->setField('status',$info['status']); 
            				
            				$this->success('操作成功', 'javascript:close_dialog();', 2);

            			}else{
            				$this->error('操作失败！,该用户帐户余额不足');
            			}
            		}
            		//$tmp = $this->member_db->where()->setField('name','ThinkPHP');
            	}else{

            	}
            }
            $show_header = false;
    }
}